#ifndef __SECTION_H__
#define __SECTION_H__
#include <vector>
#include "env.h"
namespace WServer {
namespace Section {
class wserSectionNode {
public:
	std::string prefix;
	std::string key;
	std::string value;
	struct wserSectionNode* parent;
	struct wserSectionNode* sibling;
	struct wserSectionNode* child;
	std::string getkeyname() {
		return value.length()?key:key.substr(0,key.length()-1);    //返回后面不带|的名字。
	}
	std::string getvalue() {
		//去掉两边的空格和tab。。
		int sp = value.find_first_not_of(' ');
		int tb = value.find_first_not_of('\t');
		sp = sp>tb?sp:tb;
		if (!(sp>=0))
		{
			return value;//value no valid char;
		}
		int esp = value.find_last_not_of(' ');
		int etb = value.find_last_not_of('\t');
		esp = esp<etb?esp:etb;
		return value.substr(sp,esp-sp+1);

	}
};

class wserSection
{
public:
	wserSection();
	wserSection(std::string filename);
	~wserSection();

	int OpenFile(std::string filename);
	std::string GetString(std::string key);
	int GetInteger(std::string key);
	struct wserSectionNode* GetSubNode(std::string key);
	struct wserSectionNode* GetFirstNode();

public:

private:
	struct wserSectionNode* NewNode(std::string prefix, std::string key, std::string value);
	int ReadKeylist(int hfile);
	int ReadSections(int hfile);
	struct wserSectionNode* MatchSection(struct wserSectionNode* recursive, std::string name);
	struct wserSectionNode* GetSibling(struct wserSectionNode* recursive);
	int ReadData(int hfile, void* data, int len);
	void DeleteNode(struct wserSectionNode* node);

private:
	struct wserSectionNode* root;
	std::vector<std::string> keylist;
	char memdata[MAX_SIZE];
	int remain, curpos, filesize;
	bool bend;
};
}

namespace GData {
extern Section::wserSection* gOption;
}
}

#endif //__SECTION_H__
